/**
 * Copyright (C) 2010 altuure <altuure [AT] gmail [DOT] com> http://www.altuure.com/projects/yagdao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.altuure.yagdao;

import java.io.Serializable;

/**
 * Generic resuable DAO for both hibernate and JPA.
 * @author altuure
 * @param <T> entity class
 * @param <ID> entity id class
 */
public interface GenericDAO<T, ID extends Serializable> {

    /**
     * save entity to the persistance layer.
     * @param entity entity
     */
    void save(T entity);

    /**
     * get with select.
     * @param id object id
     * @return null if not exists
     */
    T get(ID id);

    /**
     * Load entity from persitance layer. Lazy Load JPA getReference hibernate
     * load
     * @param id object id
     * @return entity
     */
    T load(ID id);

    /**
     * Load entity from persitance layer with additional relational properties.
     * @param id object id
     * @return entity
     */
    T fetch(ID id);

    /**
     * Fetch All .
     * @return list of entity
     */
    SearchResultList<T> fetchAll();

    /**
     * Fetch All .
     * @param page paging for result set
     * @return list of entity
     */
    SearchResultList<T> fetchAll(YPage page);

    /**
     * list of all entities.
     * @return list of entites
     */
    SearchResultList<T> getAll();

    /**
     * Entity class for DAO.
     * @return handled object
     */
    Class<?> getObjectClass();

    /**
     * load all with given paging.
     * @param paging paging
     * @return list of entities
     */
    SearchResultList<T> getAll(YPage paging);

    /**
     * count of all entites.
     * @return entity count
     */
    long count();

    /**
     * update entity.
     * @param entity
     */
    void update(T entity);

    /**
     * delete entity by id.
     * @param id entity id
     */
    void deleteById(ID id);

    /**
     * detach entity from session transaction.
     * @param entity entity to be detached
     */
    void detach(T entity);

    /**
     * release entity.
     * @param entity entity to be deleted
     */
    void delete(T entity);

    /**
     * returns the id property name.
     * @return id field
     */
    public String getIdProperty();
}
